package com.zzz.job.handler;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xxl.job.core.handler.annotation.XxlJob;
import com.zzz.common.core.constants.CacheConstants;
import com.zzz.common.core.constants.Constants;
import com.zzz.common.redis.service.RedisService;
import com.zzz.job.domain.exam.Exam;
import com.zzz.job.mapper.exam.ExamMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @BelongsProject: oj-project
 * @BelongsPackage: com.zzz.job.handler
 * @Author: zhai
 * @CreateTime: 2025-11-02  18:23
 * @Description: TODO
 * @Version: 1.0
 */
@Slf4j
@Component
public class ExamXxlJob {

    @Autowired
    private ExamMapper examMapper;

    @Autowired
    private RedisService redisService;

    /**
     * @description: 竞赛定时任务：刷新三个缓存：未完赛、历史、用户竞赛列表
     * @author: zhai
     * @date: 2025/11/3 22:17
     * @param: []
     * @return: void
     **/
    @XxlJob("examListOrganizeHandler")
    public void examListOrganizeHandler() {
        log.info("开始执行定时任务：刷新三个缓存：未完赛、历史、用户竞赛列表");
        // 在这里去边写刷新逻辑。如果是未完赛、历史竞赛、用户竞赛列表进行刷新即可
        List<Exam> unFinishList = examMapper.selectList(new LambdaQueryWrapper<Exam>()
                .select(Exam::getExamId, Exam::getTitle, Exam::getStartTime, Exam::getEndTime)
                .gt(Exam::getEndTime, LocalDateTime.now())
                .eq(Exam::getStatus, Constants.TRUE)
                .orderByDesc(Exam::getCreateTime));
        refreshCache(CacheConstants.EXAM_UNFINISHED_LIST, unFinishList);
        List<Exam> historyList = examMapper.selectList(new LambdaQueryWrapper<Exam>()
                .select(Exam::getExamId, Exam::getTitle, Exam::getStartTime, Exam::getEndTime)
                .le(Exam::getEndTime, LocalDateTime.now())
                .eq(Exam::getStatus, Constants.TRUE)
                .orderByDesc(Exam::getCreateTime));
        refreshCache(CacheConstants.EXAM_HISTORY_LIST, historyList);
    }

    /**
     * 刷新缓存
     * @param examListKey
     * @param examList
     */
    public void refreshCache(String examListKey, List<Exam> examList) {
        if (CollectionUtil.isEmpty(examList)) {
            log.warn("当前缓存中没有数据");
            return;
        }
        Map<String, Exam> examMap = new HashMap<>();
        List<Long> examIdList = new ArrayList<>();
        for (Exam exam : examList) {
            examMap.put(getDetailKey(exam.getExamId()), exam);
            examIdList.add(exam.getExamId());
        }
        redisService.multiSet(examMap);  //刷新详情缓存
        redisService.deleteObject(examListKey);
        redisService.rightPushAll(examListKey, examIdList); //刷新列表缓存
    }
    /**
     * 获取详情缓存key
     * @param examId
     * @return
     */
    private String getDetailKey(Long examId) {
        return CacheConstants.EXAM_DETAIL + examId;
    }

}
